package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.base.utils.CacheUtil;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboAddParam;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/weibo/")
@Api(tags = "02.微博模块")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private CacheUtil cacheUtil;

    @PostMapping("insert")
    @ApiOperation(value = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, @ApiIgnore HttpSession session) {
        log.debug("weiboAddParam: " + weiboAddParam);
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam, weibo);
        weibo.setCreateTime(new Date());
        UserVO userVO = (UserVO) session.getAttribute("user");
        weibo.setUserId(userVO.getId());
        log.debug("weibo: " + weibo);
        int count = weiboMapper.insertWeibo(weibo);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

//    @GetMapping("selectIndex")
//    @ApiOperation(value = "微博首页列表")
//    @ApiOperationSupport(order = 20)
//    public JsonResult selectIndex() {
//        List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectWeiboIndex();
//        return weiboIndexVOList != null ? JsonResult.ok(weiboIndexVOList) : new JsonResult(StatusCode.OPERATION_FAILED);
//    }

    @GetMapping("selectIndex")
    @ApiOperation(value = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex() {
        Serializable weiboIndex = cacheUtil.get("weibo_index");
        if (weiboIndex == null) {
            log.debug("数据来源于MySql数据库");
            List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectWeiboIndex();
            cacheUtil.setExpire("weibo_index", (Serializable) weiboIndexVOList, 30);
            return JsonResult.ok(weiboIndexVOList);
        } else {
            log.debug("数据来源于Redis数据库");
            return JsonResult.ok(weiboIndex);
        }
    }

    @GetMapping("selectById")
    @ApiOperation(value = "微博详情页")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "微博编号", required = true)
    public JsonResult selectById(@Range(min = 200, max = 500, message = "微博编号在200-500之间") @RequestParam Long id) {
        log.debug("微博编号: " + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboDetailById(id);
        return weiboDetailVO != null ? JsonResult.ok(weiboDetailVO) : new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
